<?php
namespace Home\Controller;

class EmployeeController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function add()
    {
        if(IS_POST){
            $employeeModel = D('Employee');
            if($employeeModel->create()){
                if($employeeModel->add()){
                    $this->success('添加成功');
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($employeeModel->getError());
            }
        }else{
            $this->assign('action', U('Employee/add'));
            $this->display();            
        }
    }
    
    public function edit()
    {
        $employeeModel = D('Employee');
        if(IS_POST){
            if($employeeModel->create()){
                if($employeeModel->save()){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败' . $employeeModel->_sql());
                }
            }else{
                $this->error($employeeModel->getError());
            }
        }else{
            $id = I('GET.id', 0, 'intval');
            if(!$id) $this->error('参数有误');
            $where = array('emp_id' => $id);
            $info = $employeeModel->where($where)->find();
            if($info){
                $this->assign('info', $info);
            }else{
                $this->error('员工不存在');
            }
            $this->assign('action', U('Employee/edit'));
            $this->display('add');
        }
    }
    
    public function lists()
    {
        $employeeModel = D('Employee');
        $employees = $employeeModel->select();
        $this->assign('users', $employees);
        $this->display();
    }
    
}